/**
 * @author wcj
 * @Date 2021/6/2 10:18
 * @Version 1.0
 */
public class FinalizeGcEscape {

    private static FinalizeGcEscape obj = null;

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("finalize method execute!");
        obj = this;
    }

    public static void main(String[] args) throws InterruptedException {
        obj = new FinalizeGcEscape();

        // 第一次测试
        obj = null;
        System.gc();
        Thread.sleep(500);
        if (obj != null) {
            System.out.println("I'm still alive!");
        } else {
            System.out.println("I'm dead");
        }

        // 第二次测试
        obj = null;
        System.gc();
        Thread.sleep(500);
        if (obj != null) {
            System.out.println("I'm still alive!");
        } else {
            System.out.println("I'm dead");
        }
    }
}
